<!DOCTYPE html>
<html style="height: 100%">

<head>
  <meta charset="utf-8">
</head>

<body style="height: 100%; margin: 0">
  <div id="container" style="height: 100%"></div>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-gl/echarts-gl.min.js"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-stat/ecStat.min.js"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/extension/dataTool.min.js"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/china.js"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/world.js"></script>
  <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=ZUONbpqGBsYGXNIYHicvbAbM"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/extension/bmap.min.js"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/simplex.js"></script>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
    var dom = document.getElementById("container");
    var myChart = echarts.init(dom);
    var timeFn = null;
    var sourceData = {
      nodes: [
        { "name": "0", "label": "中国", "category": 0, "flag": 1 },
        { "name": "1", "label": "对外贸易", "category": 1, "flag": 0 },
        { "name": "2", "label": "国家", "category": 2, "flag": 0 },
        { "name": "3", "label": "美国", "category": 3, "flag": 0 },
        { "name": "4", "label": "日本", "category": 3, "flag": 0 },
        { "name": "5", "label": "英国", "category": 3, "flag": 0 },
        { "name": "6", "label": "法国", "category": 3, "flag": 0 },
        { "name": "7", "label": "中国", "category": 3, "flag": 0 },
        { "name": "14", "label": "南非", "category": 3, "flag": 0 },
        { "name": "15", "label": "基建工程年鉴", "category": 1, "flag": 0 },
        { "name": "16", "label": "60年代", "category": 2, "flag": 0 },
        { "name": "17", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "18", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "19", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "20", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "21", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "22", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "23", "label": "70年代", "category": 2, "flag": 0 },
        { "name": "24", "label": "80年代", "category": 2, "flag": 0 },
        { "name": "25", "label": "90年代", "category": 2, "flag": 0 },
        { "name": "26", "label": "00年代", "category": 2, "flag": 0 },
        { "name": "27", "label": "10年代", "category": 2, "flag": 0 },
        { "name": "28", "label": "气候状况", "category": 1, "flag": 0 },
        { "name": "29", "label": "大气", "category": 2, "flag": 0 },
        { "name": "30", "label": "海洋", "category": 2, "flag": 0 },
        { "name": "31", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "32", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "33", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "34", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "35", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "36", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "37", "label": "山脉", "category": 2, "flag": 0 },
        { "name": "38", "label": "岛屿", "category": 2, "flag": 0 },
        { "name": "39", "label": "河流", "category": 2, "flag": 0 },
        { "name": "40", "label": "森林", "category": 2, "flag": 0 },
        { "name": "41", "label": "省", "category": 1, "flag": 0 },
        { "name": "42", "label": "河南", "category": 2, "flag": 0 },
        { "name": "43", "label": "郑州", "category": 3, "flag": 0 },
        { "name": "44", "label": "安阳", "category": 3, "flag": 0 },
        { "name": "45", "label": "南阳", "category": 3, "flag": 0 },
        { "name": "46", "label": "开封", "category": 3, "flag": 0 },
        { "name": "47", "label": "荥阳", "category": 3, "flag": 0 },
        { "name": "48", "label": "北京", "category": 2, "flag": 0 },
        { "name": "49", "label": "西城", "category": 3, "flag": 0 },
        { "name": "50", "label": "山东", "category": 2, "flag": 0 },
        { "name": "51", "label": "海南", "category": 2, "flag": 0 },
        { "name": "52", "label": "江苏", "category": 2, "flag": 0 },
        { "name": "53", "label": "台湾", "category": 2, "flag": 0 },
        { "name": "54", "label": "人口", "category": 1, "flag": 0 },
        { "name": "55", "label": "60年代", "category": 2, "flag": 0 },
        { "name": "56", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "57", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "58", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "59", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "60", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "61", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "62", "label": "70年代", "category": 2, "flag": 0 },
        { "name": "63", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "64", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "65", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "66", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "67", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "68", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "69", "label": "80年代", "category": 2, "flag": 0 },
        { "name": "70", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "71", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "72", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "73", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "74", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "75", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "76", "label": "90年代", "category": 2, "flag": 0 },
        { "name": "77", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "78", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "79", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "80", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "81", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "82", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "83", "label": "00年代", "category": 2, "flag": 0 },
        { "name": "84", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "85", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "86", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "87", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "88", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "89", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "90", "label": "10年代", "category": 2, "flag": 0 },
        { "name": "91", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "92", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "93", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "94", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "95", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "96", "label": "子主题6", "category": 3, "flag": 0 },
        { "name": "97", "label": "港口", "category": 1, "flag": 0 },
        { "name": "98", "label": "城市", "category": 2, "flag": 0 },
        { "name": "99", "label": "子主题1", "category": 3, "flag": 0 },
        { "name": "100", "label": "子主题2", "category": 3, "flag": 0 },
        { "name": "101", "label": "子主题3", "category": 3, "flag": 0 },
        { "name": "102", "label": "子主题4", "category": 3, "flag": 0 },
        { "name": "103", "label": "子主题5", "category": 3, "flag": 0 },
        { "name": "104", "label": "子主题6", "category": 3, "flag": 0 }
      ],
      links: [
        { "source": "0", "target": "1" },
        { "source": "1", "target": "2" },
        { "source": "2", "target": "3" },
        { "source": "2", "target": "4" },
        { "source": "2", "target": "5" },
        { "source": "2", "target": "6" },
        { "source": "2", "target": "7" },
        { "source": "2", "target": "14" },
        { "source": "0", "target": "15" },
        { "source": "15", "target": "16" },
        { "source": "16", "target": "17" },
        { "source": "16", "target": "18" },
        { "source": "16", "target": "19" },
        { "source": "16", "target": "20" },
        { "source": "16", "target": "21" },
        { "source": "16", "target": "22" },
        { "source": "15", "target": "23" },
        { "source": "15", "target": "24" },
        { "source": "15", "target": "25" },
        { "source": "15", "target": "26" },
        { "source": "15", "target": "27" },
        { "source": "0", "target": "28" },
        { "source": "28", "target": "29" },
        { "source": "28", "target": "30" },
        { "source": "30", "target": "31" },
        { "source": "30", "target": "32" },
        { "source": "30", "target": "33" },
        { "source": "30", "target": "34" },
        { "source": "30", "target": "35" },
        { "source": "30", "target": "36" },
        { "source": "28", "target": "37" },
        { "source": "28", "target": "38" },
        { "source": "28", "target": "39" },
        { "source": "28", "target": "40" },
        { "source": "0", "target": "41" },
        { "source": "41", "target": "42" },
        { "source": "42", "target": "43" },
        { "source": "42", "target": "44" },
        { "source": "42", "target": "45" },
        { "source": "42", "target": "46" },
        { "source": "42", "target": "47" },
        { "source": "41", "target": "48" },
        { "source": "48", "target": "49" },
        { "source": "41", "target": "50" },
        { "source": "41", "target": "51" },
        { "source": "41", "target": "52" },
        { "source": "41", "target": "53" },
        { "source": "0", "target": "54" },
        { "source": "54", "target": "55" },
        { "source": "55", "target": "56" },
        { "source": "55", "target": "57" },
        { "source": "55", "target": "58" },
        { "source": "55", "target": "59" },
        { "source": "55", "target": "60" },
        { "source": "55", "target": "61" },
        { "source": "54", "target": "62" },
        { "source": "62", "target": "63" },
        { "source": "62", "target": "64" },
        { "source": "62", "target": "65" },
        { "source": "62", "target": "66" },
        { "source": "62", "target": "67" },
        { "source": "62", "target": "68" },
        { "source": "54", "target": "69" },
        { "source": "69", "target": "70" },
        { "source": "69", "target": "71" },
        { "source": "69", "target": "72" },
        { "source": "69", "target": "73" },
        { "source": "69", "target": "74" },
        { "source": "69", "target": "75" },
        { "source": "54", "target": "76" },
        { "source": "76", "target": "77" },
        { "source": "76", "target": "78" },
        { "source": "76", "target": "79" },
        { "source": "76", "target": "80" },
        { "source": "76", "target": "81" },
        { "source": "76", "target": "82" },
        { "source": "54", "target": "83" },
        { "source": "83", "target": "84" },
        { "source": "83", "target": "85" },
        { "source": "83", "target": "86" },
        { "source": "83", "target": "87" },
        { "source": "83", "target": "88" },
        { "source": "83", "target": "89" },
        { "source": "54", "target": "90" },
        { "source": "90", "target": "91" },
        { "source": "90", "target": "92" },
        { "source": "90", "target": "93" },
        { "source": "90", "target": "94" },
        { "source": "90", "target": "95" },
        { "source": "90", "target": "96" },
        { "source": "0", "target": "97" },
        { "source": "97", "target": "98" },
        { "source": "98", "target": "99" },
        { "source": "98", "target": "100" },
        { "source": "98", "target": "101" },
        { "source": "98", "target": "102" },
        { "source": "98", "target": "103" },
        { "source": "98", "target": "104" }
      ]
    };
    // sourceData.nodes[0].fixed = true
    // sourceData.nodes[0].x = $(document).width() / 2
    // sourceData.nodes[0].y = $(document).height() / 2


    sourceData.nodes[1].fixed = true
    sourceData.nodes[1].x = $(document).width() / 3
    sourceData.nodes[1].y = $(document).height() / 3
    var graphOpt = createGraphOption(sourceData);
    myChart.setOption(graphOpt);

    //创建option
    function createGraphOption(data) {
      var nodes = data.nodes,
        links = data.links;

      var colorArray = ["#2e2e2e", "#5b9bd5", "#70ad47", "#f6ae8f", "#ffc000"];

      //获取子节点数
      for (var i in nodes) {
        var childrenNum = 0;
        for (var j in links) {
          if (nodes[i].name == links[j].source) {
            childrenNum++;
          }
        }
        nodes[i].childrenNum = childrenNum;
        nodes[i].itemStyle = {
          normal: {
            color: colorArray[nodes[i].category]
          }
        };
      }

      //初始化节点与连线数据
      var initNodes = [nodes[0]];
      var initLinks = links.filter(function (i) {
        if (i.source == "0") {
          for (var j in nodes) {
            if (i.target == nodes[j].name) {
              initNodes.push(nodes[j]);
            }
          }
          return true;
        }
      })

      var option = {
        tooltip: {},
        animationEasingUpdate: 'quinticInOut',
        // hoverAnimation: true,
        label: {
          normal: {
            show: true,
            textStyle: {
              fontSize: 12
            },
          }
        },
        series: [
          {
            type: 'graph',
            layout: 'force',
            symbolSize: 45,
            roam: true,
            tooltip: {
              trigger: 'item',
              formatter: function (params) {
                return params.data.label + ": " + params.data.childrenNum;
              }
            },

            lineStyle: {
              normal: {
                opacity: 0.9,
                width: 1.5,
                curveness: 0.5
              }
            },
            label: {
              normal: {
                show: true,
                formatter: function (params) {
                  if (params.data.childrenNum) {
                    return params.data.label + "\n" + params.data.childrenNum + '>>';
                  } else {
                    return params.data.label;
                  }

                }
              }
            },

            edgeSymbolSize: [0, 5],
            edgeSymbol: 'arrow',

            nodes: initNodes,
            links: initLinks,
            draggable: true,
            force: {
              repulsion: 500,
              // initLayout:'circular',
              layoutAnimation: false,
            }

          }
        ]
      };
      return option;
    }

    //点击事件
    myChart.on('click', cevent);

    function cevent(param) {
      let options = myChart.getOption();
      options.series[0].nodes[1].fixed = true
      options.series[0].nodes[1].x = 100
      options.series[0].nodes[1].y = 100


      myChart.setOption(options);
      //   clearTimeout(timeFn);
      //   //执行延时
      //   timeFn = setTimeout(function () {
      //     var option = myChart.getOption();
      //     var data = param.data,
      //       nodes = sourceData.nodes,
      //       links = sourceData.links;
      //     console.log('click data:', data, option.series[0].links, option.series[0].nodes);

      //     if (data != null && data != undefined) {
      //       var cName = data.name,
      //         chartNodes = option.series[0].nodes,
      //         chartLinks = option.series[0].links;

      //       // if(data.childrenNum < 1) return false;
      //       if (cName != 0) {
      //         var childNodes = [];
      //         for (var i in chartLinks) {
      //           if (cName == chartLinks[i].source) {
      //             childNodes.push(chartLinks[i].target)
      //           }
      //         }
      //         console.log('childNodes :', childNodes.length);
      //         if (childNodes === undefined || childNodes.length == 0) {
      //           var foundChild = false;
      //           console.log('add childNodes', links, chartLinks, chartNodes);
      //           for (var i in links) {
      //             if (cName == links[i].source) {
      //               chartLinks.push(links[i]);
      //               for (var j in nodes) {
      //                 if (links[i].target == nodes[j].name) {
      //                   console.log('add new node :', nodes[j]);
      //                   chartNodes.push(nodes[j]);
      //                   break;
      //                 }
      //               }
      //               foundChild = true;
      //             }
      //           }
      //           if (foundChild === false) {
      //             console.log('dynamic childs');
      //             // no child, search through the initial tree for childNodes with the same parentNode name
      //             var curr_path = getSourcePath(cName, '中国', chartLinks, chartNodes);
      //             console.log('after getSourcePath', chartNodes);
      //             var childNodes = getNodesByPath(curr_path, chartLinks, chartNodes);
      //             console.log('after getNodesByPath', chartNodes);
      //             var childLinks = getLinksByPath(curr_path, chartLinks, chartNodes);
      //             console.log('after getLinksByPath', chartNodes);
      //             var newNodes = incrementNewNodes(childNodes, chartNodes);
      //             console.log('after incrementNewNodes', chartNodes);
      //             var newLinks = newLinksByName(cName, newNodes);
      //             console.log('before concat:', childLinks, childNodes, chartLinks, chartNodes);
      //             chartLinks = chartLinks.concat(newLinks);
      //             chartNodes = chartNodes.concat(newNodes);
      //             console.log('after concat:', newNodes, newLinksByName(cName, newNodes), chartLinks, chartNodes);
      //           }
      //         } else {
      //           console.log('start remove :', chartLinks, chartNodes);

      //           var removingNodeIndexes = [];
      //           var removingLinkIndexes = [];

      //           function removeChildren(tmpLinks, source) {
      //             for (var i in tmpLinks) {
      //               if (tmpLinks[i] == undefined) {
      //                 break;
      //               }
      //               if (tmpLinks[i].source == source) {
      //                 // recursively remove children where source == this target
      //                 removeChildren(tmpLinks, tmpLinks[i].target);
      //               }
      //             }
      //             // removing Nodes
      //             chartNodes.forEach(function (e, i) {
      //               if (e.name == source && e.name != cName) {
      //                 removingNodeIndexes.push(i);
      //               }
      //             })
      //             // removing Links
      //             tmpLinks.forEach(function (e, i) {
      //               if (e.source == source) {
      //                 removingLinkIndexes.push(i);
      //               }
      //             })
      //           }

      //           removeChildren(chartLinks, cName)

      //           if (removingNodeIndexes.length > 0) {
      //             chartNodes = chartNodes.filter(function (e, i) {
      //               return !removingNodeIndexes.includes(i);
      //             })
      //           }
      //           if (removingLinkIndexes.length > 0) {
      //             chartLinks = chartLinks.filter(function (e, i) {
      //               return !removingLinkIndexes.includes(i);
      //             })
      //           }

      //           console.log('show removingIndexes', removingLinkIndexes, removingNodeIndexes);
      //           console.log('show filteredChart ', chartLinks, chartNodes);
      //         }
      //       } else {
      //         console.log('chartLinks1 :', chartLinks);

      //         //循环获取子节点
      //         function getChildren(name) {
      //           for (var i in chartLinks) {
      //             if (name == chartLinks[i].source) {
      //               transferLinksData = chartLinks;
      //               transferNodesData = chartNodes;
      //               getChildren(chartLinks[i].target);
      //             }
      //           }
      //         }

      //         //获取应该隐藏的子节点数据
      //         var transferNodesData = [],
      //           transferLinksData = [];
      //         getChildren(cName);
      //         // console.log(transferNodesData, transferLinksData);
      //         //过滤点应该隐藏的子节点数据
      //         chartNodes = chartNodes.filter(function (i) {
      //           var m = 0;
      //           for (var j in transferNodesData) {
      //             if (transferNodesData[j] == i.Name) {
      //               m = 1;
      //               break;
      //             }
      //           }
      //           return (m == 0 ? true : false);
      //         });


      //         chartLinks = chartLinks.filter(function (i) {
      //           var m = 0;
      //           for (var j in transferLinksData) {
      //             if (transferLinksData[j].source == i.source && transferLinksData[j].target == i.target) {
      //               m = 1;
      //               break;
      //             }
      //           }
      //           return (m == 1 ? true : false);
      //         });
      //       }
      //       console.log('chartLinks2 :', chartLinks);

      //       console.log('chartNodes2 :', chartNodes);
      //       //排序节点
      //       option.series[0].nodes = quicksort(chartNodes, 'name');
      //       option.series[0].links = chartLinks;

      //       //重绘图形
      //       myChart.clear();
      //       myChart.setOption(option);
      //     }
      //   }, 300);
    }

    //排序
    function quicksort(arr, key = 'name') {
      if (arr.length == 0)
        return [];

      var left = new Array();
      var right = new Array();
      var randomKey = Math.floor(Math.random() * arr.length);
      var pivot = arr[randomKey];
      var pivotArr = [];

      var pivotValue = pivot[key] == Number(pivot[key]) ? Number(pivot[key]) : pivot[key];
      for (var i = 0; i < arr.length; i++) {
        var thisValue = arr[i][key] == Number(arr[i][key]) ? Number(arr[i][key]) : arr[i][key];

        if (thisValue < pivotValue) {
          left.push(arr[i]);
        } else if (thisValue > pivotValue) {
          right.push(arr[i]);
        } else if (i != randomKey) {
          pivotArr.push(arr[i])
        }
      }

      // console.log('quicksort :', arr.length, left.length, right.length, pivot, left, right);

      return quicksort(left, key).concat(pivot, pivotArr, quicksort(right, key));
    }

    function getNameByLabel(label, nodes) {
      var nodes = nodes || sourceData.nodes;
      var found = nodes.find(function (e, i) {
        return e.label == label;
      });
      return found.name;
    }

    function getLabelByName(name, nodes) {
      function getLabel(target, nodes) {
        console.log('arguments :', Array.prototype.slice.call(arguments));
        var start = 0,
          end = nodes.length - 1,
          label = '';
        while (start <= end) {
          var mid = parseInt(start + (end - start) / 2),
            middleValue = Number(nodes[mid].name);
          // console.log('mid :', start, mid, end, target, middleValue, target>middleValue);
          if (target == middleValue) {
            label = nodes[mid].label;
            break;
          } else if (target > middleValue) {
            start = mid + 1;
          } else {
            end = mid - 1;
          }
        }
        return label;
      }
      var nodes = quicksort(nodes || sourceData.nodes, 'name'),
        ret = getLabel(name, nodes);
      console.log('results :', ret);
      return ret;
    }

    function getNodesByPath(path, links, nodes) {
      var links = quicksort(uniqueLinks(links.concat(sourceData.links)), 'target'),
        nodes = quicksort(uniqueNodes(nodes.concat(sourceData.nodes)), 'name'),
        name = getNameByPath(path, links),
        names = getChildNames(name, links),
        tmpNodes = getNodesByNames(names, nodes);
      return tmpNodes;
    }

    function getLinksByPath(path, links, nodes) {
      var links = quicksort(uniqueLinks(links.concat(sourceData.links)), 'target'),
        name = getNameByPath(path, links),
        names = getChildNames(name, links),
        links = getLinksByNames(names, links);
      return links;
    }

    function getNameByPath(path, links) {
      var links = quicksort(links || sourceData.links, 'target'),
        tmppath = path.slice(),
        parentName = getNameByLabel(tmppath.shift());
      for (var i = 0, len = tmppath.length; i < len; i++) {
        tmplinks = links.filter(function (e, i) {
          return e.source == parentName;
        });
        var childLabel = tmppath.shift(),
          childName = getNameByLabel(childLabel),
          child = tmplinks.find(function (e, i) {
            return e.target == childName;
          });
        parentName = childName;
      }
      return parentName;
    }

    function getChildNames(parentName, links) {
      var links = quicksort(links || sourceData.links, 'target'),
        nodes = [];
      for (var i in links) {
        if (links[i].source == parentName) {
          nodes.push(links[i].target);
        }
      }
      return nodes;
    }

    function getNodesByNames(names, nodes) {
      var nodes = quicksort(nodes || sourceData.nodes, 'name'),
        tmp = [];
      for (var i in nodes) {
        if (names.includes(nodes[i].name)) {
          tmp.push(nodes[i]);
        }
      }
      return tmp;
    }

    function getLinksByNames(names, links) {
      var links = quicksort(links || sourceData.links, 'target'),
        tmp = [];
      for (var i in links) {
        if (names.includes(links[i].target)) {
          tmp.push(objcp(links[i]));
        }
      }
      return tmp;
    }

    function getMaxName(nodes) {
      names = nodes.map(function (e, i) {
        return Number(e.name);
      });
      return Math.max(...names);
    }

    function incrementNewNodes(newNodes, chartNodes) {
      var tmpNodes = [];
      var maxName = getMaxName(sourceData.nodes);
      var maxName2 = getMaxName(chartNodes);
      var maxName = Math.max(maxName, maxName2);
      for (var i in newNodes) {
        let tmp = objcp(newNodes[i]);
        tmp.name = String(++maxName);
        tmpNodes.push(tmp);
      }
      // console.log(tmpNodes);
      return tmpNodes;
    }

    function newLinksByName(name, newNodes) {
      var newLinks = [];
      for (var i in newNodes) {
        newLinks[i] = {};
        newLinks[i].source = String(name);
        newLinks[i].target = String(newNodes[i].name);
      }
      // console.log(newLinks);
      return newLinks;
    }

    function getSourcePath(target, source = 0, links, tmpNodes) {
      var nodes = tmpNodes.slice() || [];
      function getSource(links, target, sourceLabel = 0) {
        console.log('arguments:', Array.prototype.slice.call(arguments));
        var targetLabel = getLabelByName(target, nodes);
        if (targetLabel == sourceLabel) {
          console.log('found root :', [source], sourceLabel);
          return [sourceLabel];
        }
        var start = 0,
          paths = [targetLabel],
          end = links.length - 1;
        while (start <= end) {
          var mid = parseInt(start + (end - start) / 2),
            middleValue = Number(links[mid].target);
          console.log('mid :', start, mid, end, target, middleValue);
          if (target == middleValue) {
            paths = paths.concat(getSource(links, Number(links[mid].source), sourceLabel));
            console.log('found target:', target, links[mid].source, paths);
            break;
          } else if (target > middleValue) {
            start = mid + 1;
          } else {
            end = mid - 1;
          }
        }
        return paths;
      }
      // console.log(links);
      var links = quicksort(links || sourceData.links, 'target'),
        ret = getSource(links, target, source);
      console.log('path results :', ret);
      return ret.reverse();
    }

    // object copy
    function objcp(src) {
      return Object.assign({}, src);
    }
    function uniqueNodes(nodes) {
      var uniqueNodes = [];
      var uniqueNames = [];
      for (var i in nodes) {
        if (!uniqueNames.includes(nodes[i].name)) {
          uniqueNodes.push(objcp(nodes[i]));
        }
        uniqueNames.push(nodes[i].name);
      }
      return uniqueNodes;
    }
    function uniqueLinks(links) {
      var uniqueLinks = [];
      var uniqueTargets = [];
      for (var i in links) {
        if (!uniqueTargets.includes(links[i].target)) {
          uniqueLinks.push(objcp(links[i]));
        }
        uniqueTargets.push(links[i].target);
      }
      return uniqueLinks;
    }

// var path = getSourcePath(2, '中国');
// console.log('path:', path);
// var label_name = getNameByLabel('国家');
// console.log('label_name:', label_name);
// var path_name = getNameByPath(path);
// console.log('path_name:', path_name, path);
// var childNodes = getNodesByPath(path);
// console.log('childNodes :', childNodes);
// var childLinks = getLinksByPath(path);
// console.log('childLinks :', childLinks);


// console.log(getLabelByName(sourceData.nodes, 56));



  </script>
</body>

</html>